<template>
  <div class="touzilist">
    <el-card>
      <el-row :gutter="10" class="elrow">
        <el-col :span="2">
          <el-tag>课程名</el-tag>
        </el-col>
        <el-col :span="4">
          <el-input placeholder="请输入课程名" v-model="formData.cname" maxlength="18"></el-input>
        </el-col>
        <el-col :span="2">
          <el-tag>课程类型</el-tag>
        </el-col>
        <el-col :span="4">
          <el-select v-model="formData.level" clearable placeholder="请选择课程类型">
            <el-option value="1" label="专升本"></el-option>
            <el-option value="2" label="高升本"></el-option>
            <el-option value="3" label="高升专"></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-tag>学习方式</el-tag>
        </el-col>
        <el-col :span="4">
          <el-select v-model="formData.cway" clearable placeholder="请选学习方式">
            <el-option value="1" label="函授"></el-option>
            <el-option value="2" label="业余"></el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" icon="el-icon-search" v-on:click="getTabsList(1,4)">搜索</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="tableData" :key="updateKey" style="width: 100%" border>
          <el-table-column type="index" :index="indexMethod" label="#"></el-table-column>
          <el-table-column label="课程名称" align="center" width="180">
            <template slot-scope="scope">
          <span v-if="scope.row.edit">
            <el-input v-model="scope.row.cname" size="small"></el-input>
          </span>
              <span v-else>{{scope.row.cname}}</span>
            </template>
          </el-table-column>
          <el-table-column label="课程审核状态" align="center" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.isverify=='1'">已审核</span>
              <span v-else="scope.row.isverify==0" style="color: #fc0d6f">
               <el-button  size="mini" type="danger" circle @click="handleVerigy(scope.$index,scope.row)">未审核</el-button>
            </span>
            </template>
          </el-table-column>
          <el-table-column label="授课教师" align="center" width="100">
            <template slot-scope="scope">
              <span >{{scope.row.tname}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" width="180">
            <template slot-scope="scope">
              <span style="color:rgb(0, 208, 83)">{{scope.row.cdate  | dataFormat}}</span>
            </template>
          </el-table-column>
          <el-table-column label="课程价格" align="center" width="180">
            <template slot-scope="scope">
            <span v-if="scope.row.edit">
              <el-input v-model="scope.row.cvalue" size="small"></el-input>
            </span>
              <span v-else>
              <el-tag size="medium">￥{{ scope.row.cvalue}}</el-tag>
            </span>
            </template>
          </el-table-column>
          <el-table-column label="课程学时" align="center" width="150">
            <template slot-scope="scope">
            <span v-if="scope.row.edit">
              <el-input v-model="scope.row.chour" size="small"></el-input>
            </span>
              <span v-else>{{ scope.row.chour}}学时</span>
            </template>
          </el-table-column>
          <el-table-column label="课程容量" align="center" width="150">
            <template slot-scope="scope">
            <span v-if="scope.row.edit">
              <el-input v-model="scope.row.cnum" size="small"></el-input>
            </span>
              <span v-else>{{ scope.row.cnum}}</span>
            </template>
          </el-table-column>
          <el-table-column label="课程层次" align="center" width="110">
            <template slot-scope="scope">
            <span v-if="scope.row.edit">
              <el-select v-model="scope.row.level" placeholder="请选择" >
                <el-option label="1-专升本":value="1"></el-option>
                <el-option label="2-高起专":value="2"></el-option>
                <el-option label="3-高升专":value="3"></el-option>
              </el-select>
            </span>
              <!--专升本-->
              <span v-else-if=" scope.row.level=='1'">专升本课程</span>
              <span v-else>高起专课程</span>
            </template>
          </el-table-column>
          <el-table-column label="面向类型" align="center" width="110">
            <template slot-scope="scope">
            <span v-if="scope.row.edit">
              <el-select v-model="scope.row.cway" placeholder="请选择">
                <el-option label="1-函授":value="1"></el-option>
                <el-option label="2-业余":value="2"></el-option>
              </el-select>
            </span>
              <span v-else-if="scope.row.cway=='1'">函授</span>
              <span v-else>业余</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120px">
            <template slot-scope="scope">
              <el-button v-if="scope.row.edit" type="success" size="mini" @click="handleSave(scope.$index,scope.row)">保存</el-button>
              <el-button v-else type="primary" size="mini" @click="handleEdit(scope.$index,scope.row)">修改</el-button>
              <el-button  size="mini" type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.$index,scope.row)"></el-button>   
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <div style="text-align: center;margin-top: 30px;">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pagesize"
          :total="total"
          :current-page="currentPage"
          @current-change="current_change">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        updateKey: 0,
        tableData: [],
        formData: {
          cname: '',
          level: '',
          cway: ''
        },
        total: 0,
        pagesize: 4,
        currentPage:1,
      };
    },
    methods: {
      current_change(val){
        this.currentPage = val;
        console.log(val);
        console.log(this.currentPage);
        this.getTabsList(this.currentPage,this.pagesize);
        // console.log(this.tableData);
      },
      indexMethod(index){
        return (this.currentPage-1)*this.pagesize+index+1;
      },
      getTotalNum(){
        this.$axios
          .get("/getpagenum", {
            params:{
              cname: this.formData.cname,
              cway: this.formData.cway,
              level: this.formData.level,
              pagesize: this.pagesize
            }
          })
          .then(res => {
            console.log(res);
            this.total = res.data.data;
            console.log(this.total);
            //   console.log(this.tableData)
          });
      },
      getTabsList(currentPage,pagesize) {
        this.getTotalNum();
        this.$axios
          .get("/querylimitcourse", {
            params:{
              cname: this.formData.cname,
              cway: this.formData.cway,
              level: this.formData.level,
              currentpage :currentPage,
              pagesize: pagesize
            }
          })
          .then(res => {
            //			console.log(res)
            const data = res.data;
            this.tableData = data.data;
            console.log(this.tableData)
          });
      },
      handleEdit(index,row) {
        row.edit =  true;
        this.updateKey = Math.random();
      },
      handleSave(index,row) {
        this.$confirm("确认修改课程信息, 是否继续?", "提示", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        })
          .then(() => {
            // 移除对应索引位置的数据，可以对row进行设置向后台请求删除数据
            // 后端数据操作
            this.$axios
              .post("/updatecourse",row)
              .then(res =>{

              })
            this.$message({
              type: "success",
              message: "修改成功!"
            });
            this.updateKey = Math.random();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取修改"
            });
          });
        row.edit =  false;
      },
      handleCancel(index,row){
        row.edit = false;
      },
      handleDelete(index,row){
        this.$confirm("永久删除该课程, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            // 移除对应索引位置的数据，可以对row进行设置向后台请求删除数据
            this.tableData.splice(index, 1);
            this.$axios.get("/deletecourse", {
              params:{
                cid: row.cid
              }
            })
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.updateKey = Math.random();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
        row.edit = false;
      },
      handleVerigy(index,row){
        this.$confirm("是否审核该课程？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            // 移除对应索引位置的数据，可以对row进行设置向后台请求删除数据
            // 数据库操作
            this.$axios
              .post("/verifycourse",row)
              .then(res => {

                }
              )
            this.$message({
              type: "success",
              message: "审核成功!"
            });
            row.isverify = '1';
            this.updateKey = Math.random();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "审核取消"
            });
          });
        row.edit = false;
      },
    },
    mounted() {
      this.getTabsList(1,4);
    }
  }
</script>

<style scoped>
  .touzilist {
    margin: 10px;
  }
  .elrow{
    margin-bottom: 20px;
  }

  .el-input__inner {
    height: 33px;
    font-size: 13px;

  }

  .el-input__inner:hover {
    border-color: #e9e9e9;
  }

  .el-input__inner:focus {
    border-color: #42a5f5;
    box-shadow: none;
    transition-duration: .5s;
  }

  .el-input__inner::-webkit-input-placeholder {
    line-height: 20px;
  }
</style>
